<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/2
 * Time: 14:44
 */

namespace App\Http\Controllers\Dynamic;

use App\Http\Controllers\ApiController;
use App\Models\Dynamic\BloodMedication;
use App\Models\Dynamic\SamGlucose;
use App\Models\Dynamic\SamWear;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;

class BloodMedicationController extends ApiController
{
    /**
     * @var \App\Models\Dynamic\SamReferGlucose
     */

    protected $user;

    protected $bloodMedication;

    protected $request;

    protected $samWear;

    protected $samGlucose;

    /**
     * BloodMediationController constructor.
     * @param BloodMedication $bloodMedication
     * @param Request $request
     */
    public function __construct(User $user, BloodMedication $bloodMedication,
                                SamWear $samWear, SamGlucose $samGlucose, Request $request)
    {
        parent::__construct();

        $this->user = $user;
        $this->bloodMedication = $bloodMedication;
        $this->samWear = $samWear;
        $this->request = $request;
        $this->samGlucose = $samGlucose;
    }


    /** 0:胰岛素使用情况 1:用药情况
     * @return bool|\Illuminate\Http\JsonResponse
     */
    public function getListByUIdByTime()
    {

        $this->validate($this->request, [
            'startTime' => 'string',
            'endTime' => 'string',
            'user_id' => 'required|integer',
            'type' => 'integer',
        ]);

        $user_id = $this->request->input('user_id');
        $startTime = $this->request->input('startTime');
        $endTime = $this->request->input('endTime');
        $type = $this->request->input('type', 0);

        $medicalInfo1 = $this->bloodMedication
            ->getInfoListByUser($user_id, $startTime, $endTime, $type);

        if (!$medicalInfo1) {
            return $this->response($medicalInfo1);
        }

        $newmedicalInfo1 = [];
        if ($medicalInfo1) {
            foreach ($medicalInfo1 as $rk => $rv) {
                $newmedicalInfo1[$rk] = array(
                    'x' => (strtotime($rv['medicationtime']) + (8 * 60 * 60)) * 1000,
                    'y' => 1,
                    'attr' => $rv['name'],
                    'marker' => ['symbol' => 'triangle']
                );
            }
        }
        return $this->response($newmedicalInfo1);
    }


    /**
     * @return \Illuminate\Http\JsonResponse
     */

    public function sportTangValue()
    {
        $this->validate($this->request, [
            'startTime' => 'string',
            'endTime' => 'string',
            'user_id' => 'required|integer',
            'id' => 'integer'
        ]);


        $user_id = $this->request->input('user_id');
        $startTime = $this->request->input('startTime');
        $endTime = $this->request->input('endTime', Carbon::now()->toDateTimeString());
        $id = $this->request->input('id');

        $data = [];
        if (!empty($id)) {
            $samWear = $this->samWear->find($id);
            if (!empty($samWear)) {
                $startTime = $samWear->starttime;
                $user_id = $samWear->userid;
                $data['samWear'] = $samWear->toArray();
                if (!empty($samWear->endtime)) {
                    $endTime = $samWear->endtime;
                }
            } else {
                abort(400, '佩戴记录不存在!');
            }
        }


        $medical = $this->bloodMedication
            ->getInfoListByUser($user_id, $startTime, $endTime, 0);


        $new_data1 = array();
        if ($medical) {
            foreach ($medical as $gk => $gv) {
                $start_time = strtotime($gv['medicationtime']);
                $end_time = $start_time + 3 * 60 * 60;
                $end_time = date("Y-m-d H:i:s", $end_time);

                $dlucose = $this->samGlucose
                    ->whereBetween('collectedtime', [$gv['medicationtime'], $end_time])
                    ->whereUserid($user_id)
                    ->get()
                    ->toArray();
                $new_data = array();
                if ($dlucose) {
                    foreach ($dlucose as $dk => $dv) {
                        $milSec = (strtotime($dv['collectedtime']) - $start_time) * 1000;
                        $bak_time = explode(' ', $dv['collectedtime']);
                        $key = $bak_time[0];
                        $new_data[$key][] = [$milSec, (float)sprintf("%1f", $dv['value'])];
                        unset($bak_time, $milSec);
                    }
                }
                foreach ($new_data as $nk => $nv) {
                    $new_data1[] = array(
                        'type' => 'spline',
                        'name' => $nk,
                        'data' => $nv,
                        'marker' => array('enabled' => false),
                        'enableMouseTracking' => true,
                    );
                }
                unset($dlucose);
            }
        }

        return $this->response($new_data1);
    }


    /** 0:胰岛素使用情况 1:用药情况
     * @return bool|\Illuminate\Http\JsonResponse
     */
    public function getListByTime()
    {

        $this->validate($this->request, [
            't' => 'string',
            'user_id' => 'required|integer',
            'type' => 'integer',
        ]);

        $user_id = $this->request->input('user_id');
        $time = $this->request->input('t', date('Y-m', time()));
        $type = $this->request->input('type', 0);

        //开始,结束
        $startTime = Carbon::parse($time)->startOfMonth()->toDateTimeString();
        $endTime = Carbon::parse($time)->endOfMonth()->toDateTimeString();


        $medicalInfo1 = $this->bloodMedication
            ->getInfoListByUser($user_id, $startTime, $endTime, $type);


        return $this->response($medicalInfo1);
    }
}